package org.funtester.performance.books.chapter05.section4;

import com.alibaba.fastjson.JSONObject;
import org.funtester.performance.books.chapter03.common.ThreadTool;
import org.funtester.performance.books.chapter03.section3.ThreadTask;
import org.funtester.performance.books.chapter03.section4.TaskExecutor;
import org.funtester.performance.books.chapter04.section3.RandomTool;
import org.funtester.performance.books.chapter05.sectioin2.JavaWebSocketClient;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * WebSocket查询商品详情接口性能测试,增加业务验证
 */
public class QueryGoodsThreadTestSecond {

    public static void main(String[] args) throws URISyntaxException, InterruptedException {
        String url = "ws://localhost:12345/websocket";// 服务端地址
        URI uri = new URI(url);// 转换为URI
        HashMap<String, String> headers = new HashMap<>();// http头信息
        headers.put("name", "user1");// 用户名
        headers.put("password", "123456");// 密码
        List<String> goods = Arrays.asList("苹果", "西瓜", "香蕉", "橘子", "梨子", "西红柿", "胡萝卜", "青椒", "洋葱", "黄瓜");// 商品名称
        AtomicInteger requestIndex = new AtomicInteger(0);// 请求索引
        Map<Integer, String> requestParams = new ConcurrentHashMap<>();
        JavaWebSocketClient client = new JavaWebSocketClient(uri, headers) {

            @Override
            public void onMessage(String s) {
                try {
                    JSONObject response = JSONObject.parseObject(s);// 解析消息
                    String name = response.getString("Name");// 获取商品名称
                    int index = response.getInteger("Index");// 获取请求索引
                    String requestName = requestParams.get(index);// 获取请求参数
                    if (!name.equalsIgnoreCase(requestName)) {// 判断商品名称是否一致
                        System.out.println("响应错误,商品名称不一致,index: " + index + ",请求参数: " + requestName + ",响应参数: " + name);
                    } else {
                        requestParams.remove(index);// 移除请求参数
                    }
                } catch (Exception e) {
                    System.out.println("响应解析异常,内容: " + s);
                    e.printStackTrace();
                }
            }
        };// 创建 WebSocket 客户端
        client.connect();// 连接 WebSocket 服务器
        int totalNum = 1000;// 总执行次数
        List<ThreadTask> tasks = new ArrayList<>();// 任务集合
        for (int i = 0; i < 8; i++) {
            ThreadTask threadTask = new ThreadTask() {// 创建任务
                @Override
                public void test() {
                    JSONObject params = new JSONObject();// 创建请求参数
                    String name = RandomTool.getRandomElement(goods);// 随机获取商品名称
                    params.put("name", name);// 设置商品名称
                    int index = requestIndex.incrementAndGet();// 获取请求索引
                    params.put("index", index);// 设置请求索引
                    requestParams.put(index, name);// 存储请求参数
                    client.send(params);// 发送消息
                    ThreadTool.sleep(10);// 等待10毫秒,控制发送频率
                }
            };
            threadTask.totalNum = totalNum;// 设置任务的总执行次数
            threadTask.costTime = new ArrayList<>(totalNum);// 设置任务的执行时间集合,设置容量,避免频繁扩容
            tasks.add(threadTask);// 将任务添加到任务集合
        }
        TaskExecutor taskExecutor = new TaskExecutor(tasks, "WebSocket查询商品详情接口性能测试", 20);
        taskExecutor.start();// 启动任务执行器
        client.close();// 关闭连接
    }

}
